﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace CharTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void OnAnalyzeStringClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtString.Text))
            {
                MessageBox.Show("Enter a string to analyze");
                return;
            }
            var str = txtString.Text;
            txtString.Text = null;
            var analysis = str
                .Distinct()
                .Select(c => new CharAnalysis(c, Images.True, Images.False))
                .ToArray();
            grdAnalysis.DataSource = analysis;
        }

        private void OnShowAllCharactersInCategoryClick(object sender, EventArgs e)
        {
            var crtCell = grdAnalysis.CurrentCell;
            if (crtCell == null)
            {
                MessageBox.Show("Select a cell");
                return;
            }
            var category = crtCell.OwningColumn.DataPropertyName;
            switch (category)
            {
                case "Char":
                case "Category":
                    MessageBox.Show("Select a category. 'Category' and 'Char' columns do not represent categories.");
                    return;
            }

            var methods = typeof(char).GetMethods(BindingFlags.Static | BindingFlags.Public);
            var method = methods.Single(m => m.Name == "Is" + category && m.GetParameters().Length == 1);
            var chars = Enumerable
                .Range(0, char.MaxValue + 1)
                .Select(i => (char)i)
                .Where(c => (bool)method.Invoke(null, new object[] { c }))
                .ToArray();

            var view = new CategoryView { Text = "Category : " + category };
            view.SetData(chars);
            view.ShowDialog();
        }
    }
}